

package com.zjdt.epserver.db.service4mysql;

import com.zjdt.epserver.db.dao.DriverDao;
import com.zjdt.epserver.model.MyConstant;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.dusin.util.DateUtil;

/**
 *
 * @author dusin
 */
public class DriverServiceImpl {
    static Logger logger = Logger.getLogger(DriverServiceImpl.class);
    DriverDao driverDao;

    public DriverServiceImpl(DriverDao driverDao) {
        this.driverDao = driverDao;
    }    
    
    public Map<String,Object> login(String username,String password){
        return driverDao.login(username, password);
    }
    
    public boolean updatePasdoUpdatePasswordByIDsword(int id,String password){
        return driverDao.doUpdatePasswordByID(id, password);
    }
    
    public boolean changePassword(String username,String oldPassword,String newPassword){
        Map<String,Object> result = driverDao.login(username, oldPassword);
        if(result!=null){
            return driverDao.doUpdatePasswordByUsername(username, newPassword);
        }
        return false;
    }
    public boolean updatePassword(int id,String newPassword){       
        return driverDao.doUpdatePasswordByID(id, newPassword);       
    }
    public boolean updatePassword(String username,String newPassword){       
        return driverDao.doUpdatePasswordByUsername(username, newPassword);       
    }
    
    public Map<String,Object> findById(int id){
        return driverDao.findById(id);
    }
    
    public Map<String,Object> findByMobilePhone(String mobilePhone){
        return driverDao.findByMobilePhone(mobilePhone);
    }
     public List<Map<String,Object>> findAll(){
        return driverDao.findAll();
    }
     
    public List<Map<String,Object>> findAll(String key){
        return driverDao.findAll(key);
    }
    
    public List<Map<String,Object>> findAll(int status){
        return driverDao.findAll(status);
    }
    
    public List<Map<String,Object>> findAll(String key,int status){
        return driverDao.findAll(key,status);
    } 
     
     public boolean create(String name,String password,String mobilePhone,String license,String licenseNo,
             String dueDate,Integer siterID){
        return driverDao.doCreate(new Object[]{mobilePhone,password,name,mobilePhone,license,licenseNo,dueDate,siterID}); 
    }
    
    public boolean update(int id,String name,String mobilePhone,String licenseNo,String dueDate){
        return driverDao.doUpdateById(new Object[]{mobilePhone,name,mobilePhone,licenseNo,dueDate,id});
    }    
    
    public boolean remove(int id){
        return driverDao.doDeleteById(id);
    }    

    public boolean updateLicensePhotoByID(int id, String license) {
        return driverDao.doUpdateLicensePhotoById(id, license);
    }
    
    public boolean updateLicensePhotoByUsername(String username,String dueDate,String license){
        return driverDao.doUpdateLicensePhotoByUsername(username,dueDate,license);
    }
    
    public boolean updateStatusByID(int id,int status){
        return driverDao.doUpdateStatusById(id, status);
    }
    
    public boolean createTrip(String rid, String startTime, int driverId, String plateNo, String fcode
            ,BigDecimal longitude,BigDecimal latitude){
        String timeStr = DateUtil.get(new Date(),"yyyy-MM-dd HH:mm:ss");
        
        driverDao.doCreateTrip(rid, startTime, driverId, plateNo, fcode, longitude,latitude);
        return driverDao.doCreateLocation(rid, longitude, latitude, timeStr, MyConstant.STARTED);
    }
    public boolean removeTrip(String rid){
        return driverDao.doDeleteTrip(rid);  //数据库设置了接连删除，删除trip将删除所有location
    }
    public boolean finishTrip(String rid,BigDecimal longitude,BigDecimal latitude,String stopTime){
        driverDao.doUpdateTripLocation(rid, longitude, latitude, stopTime,stopTime,MyConstant.WAITING);
        return driverDao.doCreateLocation(rid, longitude, latitude, stopTime, MyConstant.FINISHED);
    }
    public boolean updateLocation(String rid, BigDecimal longitude, BigDecimal latitude, String time,int status){
        driverDao.doUpdateTripLocation(rid, longitude, latitude, time,null,MyConstant.RUNNING);
        return driverDao.doCreateLocation(rid, longitude, latitude, time,status);
    }
    
    public List<Map<String,Object>> findAllTrip(int id,String date){
        return driverDao.findAllTrip(id,date);        
    }
    
    public List<Map<String,Object>> findAllHistoryTrip(int id,String date){
        return driverDao.findAllHistoryTrip(id,date);        
    }
    
    public List<Map<String,Object>> findIncomingTruck(String factoryCode){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -2);//两天前
        String date = DateUtil.get(cal.getTime(),"yyyy-MM-dd");
        return driverDao.findIncomingTruck(factoryCode, date);
    }
    
    public List<Map<String,Object>> findReachedTruck(String factoryCode){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -2);//两天前
        String date = DateUtil.get(cal.getTime(),"yyyy-MM-dd");
        return driverDao.findReachedTruck(factoryCode, date);
    }
}
